package com.vois.jack.btmgr.devices.WLBleBtnDevice;

import android.bluetooth.BluetoothGatt;
import android.os.Bundle;
import android.os.Message;
import android.os.ParcelUuid;

import com.vois.jack.btmgr.blebase.BleAction;
import com.vois.jack.btmgr.blebase.BleConstant;
import com.vois.jack.btmgr.blebase.BleDevCommonMsg;
import com.vois.jack.btmgr.blebase.BleDeviceFsm;
import com.vois.jack.btmgr.devices.WLCommonBleDev.DefaultWLBleDevice;
import com.vois.jack.btmgr.util.Logger;

import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;

public class WLBleBtnDevice extends DefaultWLBleDevice {
    private Logger logger = Logger.getLogger(WLBleBtnDevice.class);
    private static final UUID DFU_UUID = UUID.fromString("00004104-0000-1000-8000-00805F9B34FB");
    private Timer delayTimer;

    public WLBleBtnDevice() {
        super();
        setFullConnectTryTimes(12);
    }

    @Override
    public String getDeviceModel() {
        return "WLBleButton Device";
    }

    @Override
    public void onScanStateChanged(int state) {

    }

    @Override
    public void onVerifying() {

    }

//    protected String getDumpString(byte[] data, int len) {
//        char[] out = new char[len * 3];
//        final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
//                'E', 'F' };
//        for (int i = 0, j = 0; i < len; i++) {
//            out[j++] = DIGITS_UPPER[(0xF0 & data[i]) >>> 4];
//            out[j++] = DIGITS_UPPER[0x0F & data[i]];
//            out[j++] = ' ';
//        }
//
//        return new String(out);
//    }
//
//    protected void sendDebugInfo(String string) {
//        if (getListener() != null) {
//            Message message = Message.obtain();
//            message.what = BleDevCommonMsg.BLE_DEV_COMMON_DEBUG_INFO.getValue();
//            Bundle bundle = new Bundle();
//            bundle.putString("DEBUG", string);
//            message.setData(bundle);
//            getListener().onMessage(getBluetoothDevice(), message);
//        }
//    }

    @Override
    public void onCharacteristiChanged(UUID serviceUUID, UUID characteristicUUID, byte[] data) {
        super.onCharacteristiChanged(serviceUUID, characteristicUUID, data);

        if (characteristicUUID.equals(DFU_UUID)) {
            final int result = data[2] & 0xFF;
            logger.d("set dfu result is :" + result);

            delayTimer = new Timer();
            delayTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    delayTimer = null;
                    if (getListener() != null) {
                        Message message = Message.obtain();
                        message.what = BleDevCommonMsg.BLE_DEV_COMMON_DFU_SET_RESULT.getValue();
                        Bundle bundle = new Bundle();
                        bundle.putParcelable(BleConstant.EXTRA_BLUETOOTH_DEVICE, getBluetoothDevice());
                        bundle.putInt(BleConstant.EXTRA_VALUE_RESULT, result);
                        message.setData(bundle);
                        if (getListener() != null) {
                            getListener().onMessage(getBluetoothDevice(), message);
                        }
                    }
                }
            }, 1000);

        }
    }

    public void enterDfu() {
        logger.d("enterDfu");
        enableCharacteristicNotification(SERVICE_UUID, DFU_UUID, true, INDICATE_TYPE, new BleAction.BleActionCallback() {
            @Override
            public void onActionResult(int status, Bundle result) {
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    byte[] dfuData = new byte[2];
                    dfuData[0] = (byte)0xAA;
                    dfuData[1] = 0x00;
                    writeCharacteristicData(SERVICE_UUID, DFU_UUID, dfuData, 2, new BleAction.BleActionCallback() {
                        @Override
                        public void onActionResult(int status, Bundle result) {
                            if (status == BluetoothGatt.GATT_SUCCESS) {
                                logger.d("enterDfu write dfu set status:" + status);
                            }
                        }
                    });
                }
            }
        });
    }
}
